{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Working with different time zones"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2024-06-10 09:00:00+02:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2024-06-10 10:00:00+02:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2024-06-10 11:00:00+02:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2024-09-10 09:00:00-05:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2024-09-10 10:00:00-05:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2024-09-10 11:00:00-05:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       time1\n",
       "0  2024-06-10 09:00:00+02:00\n",
       "1  2024-06-10 10:00:00+02:00\n",
       "2  2024-06-10 11:00:00+02:00\n",
       "0  2024-09-10 09:00:00-05:00\n",
       "1  2024-09-10 10:00:00-05:00\n",
       "2  2024-09-10 11:00:00-05:00"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# first, let's create a toy dataframe with some timestamps in different time zones\n",
    "# variable 1\n",
    "\n",
    "df = pd.DataFrame()\n",
    "\n",
    "df[\"time1\"] = pd.concat(\n",
    "    [\n",
    "        pd.Series(\n",
    "            pd.date_range(\n",
    "                start=\"2024-06-10 09:00\", freq=\"h\", periods=3, tz=\"Europe/Berlin\"\n",
    "            )\n",
    "        ),\n",
    "        pd.Series(\n",
    "            pd.date_range(\n",
    "                start=\"2024-09-10 09:00\", freq=\"h\", periods=3, tz=\"US/Central\"\n",
    "            )\n",
    "        ),\n",
    "    ],\n",
    "    axis=0,\n",
    ")\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time1</th>\n",
       "      <th>time2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2024-06-10 09:00:00+02:00</td>\n",
       "      <td>2024-07-01 09:00:00+02:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2024-06-10 10:00:00+02:00</td>\n",
       "      <td>2024-07-01 10:00:00+02:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2024-06-10 11:00:00+02:00</td>\n",
       "      <td>2024-07-01 11:00:00+02:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2024-09-10 09:00:00-05:00</td>\n",
       "      <td>2024-08-01 09:00:00-05:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2024-09-10 10:00:00-05:00</td>\n",
       "      <td>2024-08-01 10:00:00-05:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2024-09-10 11:00:00-05:00</td>\n",
       "      <td>2024-08-01 11:00:00-05:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       time1                      time2\n",
       "0  2024-06-10 09:00:00+02:00  2024-07-01 09:00:00+02:00\n",
       "1  2024-06-10 10:00:00+02:00  2024-07-01 10:00:00+02:00\n",
       "2  2024-06-10 11:00:00+02:00  2024-07-01 11:00:00+02:00\n",
       "0  2024-09-10 09:00:00-05:00  2024-08-01 09:00:00-05:00\n",
       "1  2024-09-10 10:00:00-05:00  2024-08-01 10:00:00-05:00\n",
       "2  2024-09-10 11:00:00-05:00  2024-08-01 11:00:00-05:00"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# first, let's create a toy dataframe with some timestamps in different time zones\n",
    "# variable 2\n",
    "\n",
    "df[\"time2\"] = pd.concat(\n",
    "    [\n",
    "        pd.Series(\n",
    "            pd.date_range(\n",
    "                start=\"2024-07-01 09:00\", freq=\"h\", periods=3, tz=\"Europe/Berlin\"\n",
    "            )\n",
    "        ),\n",
    "        pd.Series(\n",
    "            pd.date_range(\n",
    "                start=\"2024-08-01 09:00\", freq=\"h\", periods=3, tz=\"US/Central\"\n",
    "            )\n",
    "        ),\n",
    "    ],\n",
    "    axis=0,\n",
    ")\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time1</th>\n",
       "      <th>time2</th>\n",
       "      <th>time1_utc</th>\n",
       "      <th>time2_utc</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2024-06-10 09:00:00+02:00</td>\n",
       "      <td>2024-07-01 09:00:00+02:00</td>\n",
       "      <td>2024-06-10 07:00:00+00:00</td>\n",
       "      <td>2024-07-01 07:00:00+00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2024-06-10 10:00:00+02:00</td>\n",
       "      <td>2024-07-01 10:00:00+02:00</td>\n",
       "      <td>2024-06-10 08:00:00+00:00</td>\n",
       "      <td>2024-07-01 08:00:00+00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2024-06-10 11:00:00+02:00</td>\n",
       "      <td>2024-07-01 11:00:00+02:00</td>\n",
       "      <td>2024-06-10 09:00:00+00:00</td>\n",
       "      <td>2024-07-01 09:00:00+00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2024-09-10 09:00:00-05:00</td>\n",
       "      <td>2024-08-01 09:00:00-05:00</td>\n",
       "      <td>2024-09-10 14:00:00+00:00</td>\n",
       "      <td>2024-08-01 14:00:00+00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2024-09-10 10:00:00-05:00</td>\n",
       "      <td>2024-08-01 10:00:00-05:00</td>\n",
       "      <td>2024-09-10 15:00:00+00:00</td>\n",
       "      <td>2024-08-01 15:00:00+00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2024-09-10 11:00:00-05:00</td>\n",
       "      <td>2024-08-01 11:00:00-05:00</td>\n",
       "      <td>2024-09-10 16:00:00+00:00</td>\n",
       "      <td>2024-08-01 16:00:00+00:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       time1                      time2  \\\n",
       "0  2024-06-10 09:00:00+02:00  2024-07-01 09:00:00+02:00   \n",
       "1  2024-06-10 10:00:00+02:00  2024-07-01 10:00:00+02:00   \n",
       "2  2024-06-10 11:00:00+02:00  2024-07-01 11:00:00+02:00   \n",
       "0  2024-09-10 09:00:00-05:00  2024-08-01 09:00:00-05:00   \n",
       "1  2024-09-10 10:00:00-05:00  2024-08-01 10:00:00-05:00   \n",
       "2  2024-09-10 11:00:00-05:00  2024-08-01 11:00:00-05:00   \n",
       "\n",
       "                  time1_utc                 time2_utc  \n",
       "0 2024-06-10 07:00:00+00:00 2024-07-01 07:00:00+00:00  \n",
       "1 2024-06-10 08:00:00+00:00 2024-07-01 08:00:00+00:00  \n",
       "2 2024-06-10 09:00:00+00:00 2024-07-01 09:00:00+00:00  \n",
       "0 2024-09-10 14:00:00+00:00 2024-08-01 14:00:00+00:00  \n",
       "1 2024-09-10 15:00:00+00:00 2024-08-01 15:00:00+00:00  \n",
       "2 2024-09-10 16:00:00+00:00 2024-08-01 16:00:00+00:00  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# to work with different time zones, first we unify the timezone to the central one\n",
    "# setting utc = True\n",
    "\n",
    "df[\"time1_utc\"] = pd.to_datetime(df[\"time1\"], utc=True)\n",
    "df[\"time2_utc\"] = pd.to_datetime(df[\"time2\"], utc=True)\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    21\n",
       "1    21\n",
       "2    21\n",
       "0   -40\n",
       "1   -40\n",
       "Name: elapsed_days, dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# let's explore the variable type\n",
    "\n",
    "df[\"elapsed_days\"] = (df[\"time2_utc\"] - df[\"time1_utc\"]).dt.days\n",
    "\n",
    "df[\"elapsed_days\"].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time1_london</th>\n",
       "      <th>time2_berlin</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2024-06-10 08:00:00+01:00</td>\n",
       "      <td>2024-06-10 09:00:00+02:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2024-06-10 09:00:00+01:00</td>\n",
       "      <td>2024-06-10 10:00:00+02:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2024-06-10 10:00:00+01:00</td>\n",
       "      <td>2024-06-10 11:00:00+02:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2024-09-10 15:00:00+01:00</td>\n",
       "      <td>2024-09-10 16:00:00+02:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2024-09-10 16:00:00+01:00</td>\n",
       "      <td>2024-09-10 17:00:00+02:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2024-09-10 17:00:00+01:00</td>\n",
       "      <td>2024-09-10 18:00:00+02:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               time1_london              time2_berlin\n",
       "0 2024-06-10 08:00:00+01:00 2024-06-10 09:00:00+02:00\n",
       "1 2024-06-10 09:00:00+01:00 2024-06-10 10:00:00+02:00\n",
       "2 2024-06-10 10:00:00+01:00 2024-06-10 11:00:00+02:00\n",
       "0 2024-09-10 15:00:00+01:00 2024-09-10 16:00:00+02:00\n",
       "1 2024-09-10 16:00:00+01:00 2024-09-10 17:00:00+02:00\n",
       "2 2024-09-10 17:00:00+01:00 2024-09-10 18:00:00+02:00"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# next we change all timestamps to the desired timezone, eg Europe/London\n",
    "# in this example\n",
    "\n",
    "df[\"time1_london\"] = df[\"time1_utc\"].dt.tz_convert(\"Europe/London\")\n",
    "df[\"time2_berlin\"] = df[\"time1_utc\"].dt.tz_convert(\"Europe/Berlin\")\n",
    "\n",
    "df[[\"time1_london\", \"time2_berlin\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "fsml",
   "language": "python",
   "name": "fsml"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
